package com.bw.controller;

import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.bw.service.FastFoodService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;

/**
 * @author 小郭同学
 * * @date 2025/2/17
 */
@RestController
public class FastFoodController {
    @Autowired
    FastFoodService fastFoodService;
    @RequestMapping("/ratingCount")
    public Object ratingCount(){
        List<Map> list = fastFoodService.ratingCount();
        //1
        JSONObject jsonObject = new JSONObject();
        //1.1
        jsonObject.put("status",0);
        //1.2
        jsonObject.put("msg","");
        //1.3
        JSONArray categoriesArray = new JSONArray();
        for (Map map : list) {
            categoriesArray.add(map.get("name"));
        }
        JSONObject categoriesObject = new JSONObject();
        categoriesObject.put("categories",categoriesArray);

        JSONArray seriesArray = new JSONArray();


        JSONObject seriesObject1 = new JSONObject();
        seriesObject1.put("name","评价次数");
        JSONArray dataArray1 = new JSONArray();
        for (Map map : list) {
            dataArray1.add(map.get("ratingCount"));
        }
        seriesObject1.put("data",dataArray1);

        JSONObject seriesObject2 = new JSONObject();
        seriesObject2.put("name","好评次数");
        JSONArray dataArray2 = new JSONArray();
        for (Map map : list) {
            dataArray2.add(map.get("goodRating"));
        }
        seriesObject2.put("data",dataArray2);

        JSONObject seriesObject3 = new JSONObject();
        seriesObject3.put("name","平均评分");
        JSONArray dataArray3 = new JSONArray();
        for (Map map : list) {
            dataArray3.add(map.get("avgRating"));
        }
        seriesObject3.put("data",dataArray3);

        JSONObject seriesObject4 = new JSONObject();
        seriesObject4.put("name","好评率");
        JSONArray dataArray4 = new JSONArray();
        for (Map map : list) {
            dataArray4.add(map.get("percent"));
        }
        seriesObject4.put("data",dataArray4);

        seriesArray.add(seriesObject1);
        seriesArray.add(seriesObject2);
        seriesArray.add(seriesObject3);
        seriesArray.add(seriesObject4);
        categoriesObject.put("series",seriesArray);

        jsonObject.put("data",categoriesObject);
        return jsonObject;
    }

    @RequestMapping("/amountSum")
    public Object amountSum(){
        List<Map> list = fastFoodService.amountSum();
        //1
        JSONObject jsonObject = new JSONObject();
        //1.1
        jsonObject.put("status",0);
        //1.2
        jsonObject.put("msg","");
        //1.3
        JSONObject dataObject = new JSONObject();
        JSONArray mapDataArray = new JSONArray();
        for (Map map : list) {
            JSONObject mapDataObject = new JSONObject();
            mapDataObject.put("name",map.get("province"));
            mapDataObject.put("value",map.get("total"));
            JSONArray array = new JSONArray();
            array.add(map.get("count"));
            array.add(map.get("person"));
            array.add(map.get("actualAmount"));
            array.add(map.get("refund"));
            mapDataObject.put("tooltipValues",array);
            mapDataArray.add(mapDataObject);
        }
        dataObject.put("mapData",mapDataArray);

        dataObject.put("valueName","下单金额");

        JSONArray tooltipNames = new JSONArray();
        tooltipNames.add("下单次数");
        tooltipNames.add("下单人数");
        tooltipNames.add("实收金额");
        tooltipNames.add("退款成功金额");
        dataObject.put("tooltipNames",tooltipNames);

        jsonObject.put("data",dataObject);

        return jsonObject;
    }

    @RequestMapping("/promotionSum")
    public Object promotionSUm(){
        List<Map> list = fastFoodService.promotionSum();
        //1
        JSONObject jsonObject = new JSONObject();
        //1.1
        jsonObject.put("status",0);
        //1.2
        jsonObject.put("msg","");
        //1.3
        JSONObject dataObject = new JSONObject();
        JSONArray columnsArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("name","最近 1/7/30 日");
        jsonObject1.put("id","day");
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("name","活动");
        jsonObject2.put("id","promotion");
        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("name","累计减免（下单）金额");
        jsonObject3.put("id","reduce");
        JSONObject jsonObject4 = new JSONObject();
        jsonObject4.put("name","订单数");
        jsonObject4.put("id","orders");
        JSONObject jsonObject5 = new JSONObject();
        jsonObject5.put("name","参与（下单）人数");
        jsonObject5.put("id","customer");
        columnsArray.add(jsonObject1);
        columnsArray.add(jsonObject2);
        columnsArray.add(jsonObject3);
        columnsArray.add(jsonObject4);
        columnsArray.add(jsonObject5);
        dataObject.put("columns",columnsArray);

        JSONArray rowsArray = new JSONArray();
        for (Map map : list) {
            JSONObject rowsObject = new JSONObject();
            rowsObject.put("day",map.get("day"));
            rowsObject.put("promotion",map.get("promotion"));
            rowsObject.put("reduce",map.get("reduce"));
            rowsObject.put("orders",map.get("orders"));
            rowsObject.put("customer",map.get("customer"));
            rowsArray.add(rowsObject);
        }
        dataObject.put("rows",rowsArray);

        jsonObject.put("data",dataObject);
        return jsonObject;
    }

    @RequestMapping("/shopRank")
    public Object shopRank(){
        List<Map> list = fastFoodService.shopRank();
        //1
        JSONObject jsonObject = new JSONObject();
        //1.1
        jsonObject.put("status",0);
        //1.2
        jsonObject.put("msg","");
        //1.3
        JSONObject dataObject = new JSONObject();
        dataObject.put("total",10);
        JSONArray columnsArray = new JSONArray();
        JSONObject jsonObject1 = new JSONObject();
        jsonObject1.put("name","店铺名称");
        jsonObject1.put("id","name");
        JSONObject jsonObject2 = new JSONObject();
        jsonObject2.put("name","下单金额");
        jsonObject2.put("id","total");
        JSONObject jsonObject3 = new JSONObject();
        jsonObject3.put("name","排名");
        jsonObject3.put("id","ranks");
        columnsArray.add(jsonObject1);
        columnsArray.add(jsonObject2);
        columnsArray.add(jsonObject3);
        dataObject.put("columns",columnsArray);

        JSONArray rowsArray = new JSONArray();
        for (Map map : list) {
            JSONObject rowsObject = new JSONObject();
            rowsObject.put("name",map.get("name"));
            rowsObject.put("total",map.get("total"));
            rowsObject.put("ranks",map.get("ranks"));
            rowsArray.add(rowsObject);
        }
        dataObject.put("rows",rowsArray);
        jsonObject.put("data",dataObject);

        return jsonObject;
    }
}
